<?php


namespace Tbf\Bundle\YntAuthBundle\Service;


use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Tbf\Bundle\SerializerBundle\Serializer;
use Tbf\Bundle\YntAuthBundle\Model\Transaction;
use Tbf\Component\Crypto\CryptoRandUtil;

class TransactionManager {
    protected $session;
    protected $serializer;
    function __construct(SessionInterface $session,
        Serializer $serializer
        ){
        $this->session = $session;
        $this->serializer = $serializer;
    }

    /**
     * @param $transaction_id
     * @return null|Transaction
     */
    function get($transaction_id){
        $data = $this->session->get('tbf_ynt_auth.'.$transaction_id);
        if (empty($data)){
            return null;
        }
        return $this->serializer->newFromArray('Tbf\Bundle\YntAuthBundle\Model\Transaction',$data);
    }

    /**
     * @param Transaction $transaction
     */
    function save(Transaction $transaction){
        $data = $this->serializer->toArray($transaction);
        $this->session->set('tbf_ynt_auth.'.$transaction->getId(),$data);
    }
    /**
     * @return Transaction
     */
    function newTransaction(){
        $id = CryptoRandUtil::hex(10);
        $transaction = new Transaction($id);
        return $transaction;
    }
}